///////////////////////////////////////////////////////////////////////
// WriteEngine2.cpp
//
// Wrapper for IWriteEngine2 Interface
//
// Written by Nic Wilson
//

#include "StdAfx.h"
#include "WriteEngine2.h"
#include "DiscRecorder.h"
#include "WriteEngine2Event.h"

CWriteEngine2::CWriteEngine2(void)
: m_WriteEngine2(NULL)
, m_mediaTypesArray(NULL)
, m_hResult(0)
, m_hNotificationWnd(NULL)
, m_closeMedia(true)
{
}

CWriteEngine2::~CWriteEngine2(void)
{
	if (m_WriteEngine2 != NULL)
	{
		m_WriteEngine2->Release();
	}
}

///////////////////////////////////////////////////////////////////////
//
// CWriteEngine2::Initialize()
//
// Description:
//		Creates and initializes the IWriteEngine2 interface
//
bool CWriteEngine2::Initialize(CDiscRecorder* pDiscRecorder, const CString& clientName)
{
	ASSERT(m_WriteEngine2 == NULL);
	ASSERT(pDiscRecorder != NULL);
	if (pDiscRecorder == NULL)
	{
		m_errorMessage = _T("Error - CWriteEngine2::Initialize - pDiscRecorder is NULL");
		return false;
	}

	//
	// Initialize the IWriteEngine2 Interface
	//
	m_hResult = CoCreateInstance(__uuidof(MsftWriteEngine2), NULL, CLSCTX_INPROC_SERVER,
			__uuidof(IWriteEngine2), (void**)&m_WriteEngine2);
	if (!SUCCEEDED(m_hResult))
	{
		m_errorMessage.Format(_T("Unable to Initialize IWriteEngine2 - Error:0x%08x"), m_hResult);
		return false;
	}
	m_hResult = m_WriteEngine2->put_Recorder(pDiscRecorder->GetIDiscRecorder2ExPtr());
	if (!SUCCEEDED(m_hResult))
	{
		m_errorMessage.Format(_T("IWriteEngine2->put_Recorder Failed - Error:0x%08x"), m_hResult);
		return false;
	}
	return true;
}

ULONG CWriteEngine2::GetTotalSupportedMediaTypes()
{
	if (m_mediaTypesArray == NULL)
		return 0;

	return m_mediaTypesArray->rgsabound[0].cElements;
}

int CWriteEngine2::GetSupportedMediaType(ULONG index)
{
	ASSERT(index < GetTotalSupportedMediaTypes());
	if (index < GetTotalSupportedMediaTypes())
	{
		if (m_mediaTypesArray)
		{
			return ((VARIANT*)(m_mediaTypesArray->pvData))[index].intVal;
		}
	}

	return 0;
}

bool CWriteEngine2::Burn(HWND hNotificationWnd, IStream* streamData, LONG startingBlockAddress, LONG numberOfBlocks)
{
	if (m_WriteEngine2 == NULL)
		return false;

	if (hNotificationWnd == NULL)
		return false;

	if (streamData == NULL)
		return false;

	m_streamData = streamData;
	m_hNotificationWnd = hNotificationWnd;
	// Create the event sink
	CWriteEngine2Event* eventSink = CWriteEngine2Event::CreateEventSink();
	if (eventSink == NULL)
	{
		m_errorMessage = _T("Unable to create event sink");
		return false;
	}

	if (!eventSink->ConnectWriteEngine2(this))
	{
		m_errorMessage = _T("Unable to connect event sink with interface");
		return false;
	}
	eventSink->SetHwnd(m_hNotificationWnd);
	m_hResult = m_WriteEngine2->WriteSection(m_streamData, startingBlockAddress, numberOfBlocks);
	delete eventSink;
	if (SUCCEEDED(m_hResult))
	{
		return true;
	}
	m_errorMessage.Format(_T("IWriteEngine2->Write Failed! Error:0x%08x"),
		m_hResult);
	return true;

}

IWriteEngine2* CWriteEngine2::GetIWriteEngine2Ptr()
{
	return m_WriteEngine2;
}

